package com.lee.chain.controller;

import com.lee.chain.service.AbstractLoggerHandler;
import com.lee.chain.service.ChainOfResponsibilityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zfl_a
 * @date 2020/8/11
 * @project springboot_design_pattern
 */
@RestController
@RequestMapping("/chain")
public class ChainOfResponsibilityController {

    @Autowired
    private ChainOfResponsibilityService chainOfResponsibilityService ;

    /**
     *
     * @param level 日志级别 级别 1INFO 2ERRPR 3DEBUGGER
     * @param kindHandler 种类 目前只有一种 log
     * @param message 消息
     * @return
     */
    @GetMapping("/logMessage")
    public Map<String,Object> logMessage(String level,String kindHandler,String message) {



        Map<String,Object> results = new HashMap<>();
        results.put("code","200");
        results.put("msg","操作成功");

        if(StringUtils.isBlank(level)) {
            results.put("code","-2");
            results.put("msg","请传递级别 1INFO 2ERRPR 3DEBUGGER");
        }

        AbstractLoggerHandler topLogHandler = chainOfResponsibilityService.findTopLogHandler(kindHandler);
        if(topLogHandler!=null) {
            topLogHandler.logMessage(Integer.parseInt(level),message);
        }
        return results ;
    }
}
